import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 周斌
 * Date: 2024-03-13
 * Time: 21:48
 */
public class GetMinStack {
   Stack<Integer> stack= new Stack<>();
   Stack<Integer> Minstack= new Stack<>();
    //push
    public void push(int val) {
        //普通栈直接压栈
        stack.push(val);
        //最小栈压栈两种情况
        if(Minstack.empty()) {
            //1.最小栈为空栈，直接压栈。
            Minstack.push(val);
        }
        else {
            //2.最小栈不为空栈，则需要将压栈的元素与最小栈栈顶元素比较，当压栈元素<=最小栈栈顶元素则可以压栈
            if(val <= Minstack.peek()) {
                Minstack.push(val);
            }
        }
    }
    //pop
    public void pop() {
        int ret = stack.pop();
        if (ret == Minstack.peek()) {
            Minstack.pop();
        }
    }
    //peek
    public int peek() {
       return stack.peek();
    }
    //getMin
    public int getMin() {
       return Minstack.peek();
    }

    public static void main(String[] args) {
        GetMinStack getMinStack = new GetMinStack();
        getMinStack.push(-2);
        getMinStack.push(0);
        getMinStack.push(-3);
        System.out.println(getMinStack.getMin());
    }
}
